package com.fwtai.service.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.tool.IdBuilder;
import com.fwtai.tool.ToolClient;
import com.fwtai.web.GoodsCategoryDao;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 产品分类业务层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @Email service@fwtai.com
 * @创建日期 2023-08-23 14:49:22
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class GoodsCategoryService{

  private final GoodsCategoryDao goodsCategoryDao;

  private final ThreadPoolTaskExecutor taskExecutor;

  public GoodsCategoryService(final GoodsCategoryDao goodsCategoryDao,final ThreadPoolTaskExecutor taskExecutor){
    this.goodsCategoryDao = goodsCategoryDao;
    this.taskExecutor = taskExecutor;
  }

  public String add(final HttpServletRequest request){
    return CompletableFuture.supplyAsync(()->{
      final PageFormData formData = new PageFormData(request);
      final String p_pid = "pid";
      final String p_name = "name";
      final String p_order_by = "order_by";
      final String validate = ToolClient.validateField(formData,p_name,p_order_by);
      if(validate != null) return validate;
      final String len_name = formData.getString(p_name);
      if(len_name != null && len_name.length() > 64){
        return ToolClient.createJsonFail("名称字数太多");
      }
      final String len_order_by = formData.getString(p_order_by);
      if(len_order_by != null && len_order_by.length() > 5){
        return ToolClient.createJsonFail("排序字数太多");
      }
      final Long v_pid = formData.getLong(p_pid);
      final long kid = new IdBuilder().getId121L();
      if(v_pid != null){
        formData.put(p_pid,v_pid);
      }else{
        formData.put(p_pid,kid);
      }
      final Integer v_order_by = formData.getInteger(p_order_by);
      if(v_order_by == null){
        return ToolClient.createJsonFail("排序不是有效的数字");
      }else{
        formData.put(p_order_by,v_order_by);
      }
      formData.put("kid",kid);
      return ToolClient.executeRows(goodsCategoryDao.add(formData));
    },taskExecutor).join();
  }

  public String edit(final HttpServletRequest request){
    return CompletableFuture.supplyAsync(()->{
      final PageFormData formData = new PageFormData(request);
      final String p_kid = "kid";
      final String p_pid = "pid";
      final String p_name = "name";
      final String p_order_by = "order_by";
      final String validate = ToolClient.validateField(formData,p_kid,p_pid,p_name,p_order_by);
      if(validate != null) return validate;
      final String len_pid = formData.getString(p_pid);
      if(len_pid != null && len_pid.length() > 19){
        return ToolClient.createJsonFail("父级id字数太多");
      }
      final String len_name = formData.getString(p_name);
      if(len_name != null && len_name.length() > 64){
        return ToolClient.createJsonFail("名称字数太多");
      }
      final String len_order_by = formData.getString(p_order_by);
      if(len_order_by != null && len_order_by.length() > 5){
        return ToolClient.createJsonFail("排序字数太多");
      }
      final Long v_pid = formData.getLong(p_pid);
      if(v_pid == null){
        return ToolClient.createJsonFail("父级id不是有效的数据格式");
      }else{
        formData.put(p_pid,v_pid);
      }
      if(len_order_by != null){
        final Integer v_order_by = formData.getInteger(p_order_by);
        if(v_order_by == null){
          return ToolClient.createJsonFail("排序不是有效的数字");
        }else{
          formData.put(p_order_by,v_order_by);
        }
      }
      final long kid = formData.getLong(p_kid);
      final Long v = goodsCategoryDao.queryExistById(kid);
      if(v == null){
        return ToolClient.createJsonFail("数据不存在,刷新重试");
      }
      formData.put(p_kid,kid);
      return ToolClient.executeRows(goodsCategoryDao.edit(formData));
    },taskExecutor).join();
  }

  public String queryById(final Long kid){
    return CompletableFuture.supplyAsync(()->{
      if(kid == null){
        return ToolClient.jsonValidateField();
      }
      return ToolClient.queryJson(goodsCategoryDao.queryById(kid));
    },taskExecutor).join();
  }

  //编辑时的文件删除可参考此方法
  public String delById(final Long kid){
    return CompletableFuture.supplyAsync(()->{
      if(kid == null){
        return ToolClient.jsonValidateField();
      }
      final List<Long> used = goodsCategoryDao.queryIsUsed(kid);
      if(used.size() > 0){
        return ToolClient.createJsonFail("操作失败,所删除的数据已被使用");
      }
      return ToolClient.executeRows(goodsCategoryDao.delById(kid),"操作成功","删除失败,刷新重试");
    },taskExecutor).join();
  }

  public String listData(final PageFormData formData){
    return CompletableFuture.supplyAsync(()->{
      try{
        if(ToolClient.dataTableMysql(formData) == null) return ToolClient.jsonValidateField();
        final CompletableFuture<List<HashMap<String,Object>>> listData = CompletableFuture.supplyAsync(() -> goodsCategoryDao.getListData(formData),taskExecutor);
        final CompletableFuture<Integer> listTotal = CompletableFuture.supplyAsync(() -> goodsCategoryDao.getListTotal(formData),taskExecutor);
        return ToolClient.dataTableOK(listData.join(),listTotal.join());
      } catch (final Exception ignored){
        return ToolClient.dataTableException();
      }
    },taskExecutor).join();
  }
}